package mess;

import javax.lang.model.element.NestingKind;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LeeCode_13 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.print("请输入您要转换的罗马数字: ");
            String str = sc.next();
            System.out.println(str + " 转换为阿拉伯数字为: " + romanToInt(str));
        }
    }
    private static int romanToInt(String s) {
        int len = s.length();
        int result = 0;
        Map<Character, Integer> table = new HashMap<>();
        table.put('I', 1);
        table.put('V', 5);
        table.put('X', 10);
        table.put('L', 50);
        table.put('C', 100);
        table.put('D', 500);
        table.put('M', 1000);
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if ((c == 'I' || c == 'X' || c == 'C') && i != len - 1 && table.get(s.charAt(i + 1)) > table.get(c)) {;
                result += table.get(s.charAt(i + 1)) - table.get(s.charAt(i));
                i += 1;
                continue;
            }
            result += table.get(c);
        }
        return result;
    }
}
